package com.zy.service;

import com.zy.dao.ManagementMapper;
import com.zy.entity.Page;
import com.zy.entity.Video;
import com.zy.entity.VideoText;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ManagementService
{

    @Autowired
    private ManagementMapper managementMapper;

    /**
     * 后台视频管理搜索视频
     * @param pageNo
     * @param pageSize
     * @param typeName
     * @param userName
     * @return
     */
    public Page<VideoText> searchVideoListByPageNoAndPageSizeAndTypeNameOrUserName(int pageNo, int pageSize, String typeName, String userName)
    {
        Page<VideoText> page = new Page<>();

        page.setPageNo(pageNo);//设置当前页码

        page.setPageSize(pageSize);//设置每页数量

        page.setPagesTotalCount(managementMapper.videoNumByTypeNameOrUserName(typeName, userName));//设置总记录数

        int pageTotal = page.getPagesTotalCount()/pageSize;

        if(page.getPagesTotalCount()%pageSize>0)
        {
            pageTotal++;
        }

        page.setPageTotal(pageTotal);//设置页码总数

        int begin = (pageNo-1)*pageSize;//起始数

        page.setItems(managementMapper.searchVideoListByBeginAndPageSizeAndTypeNameOrUserName(begin, pageSize, typeName, userName));//设置当前页数据

        return page;
    }

    /**
     * 通过id查询删除信息并返回信息
     * @param id
     */
    public void deleteVideoById(int id)
    {
        managementMapper.deleteVideoById(id);
    }

    /**
     * 统计视频每月数量
     * @param video
     * @return
     */
    public Map<String, Object> selectVideoCount(Video video)
    {
        Map<String, Object> map = new HashMap<>();
        List<Integer> countList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        countList.add(managementMapper.selectVideoCount(year+"-01-"));
        countList.add(managementMapper.selectVideoCount(year+"-02-"));
        countList.add(managementMapper.selectVideoCount(year+"-03-"));
        countList.add(managementMapper.selectVideoCount(year+"-04-"));
        countList.add(managementMapper.selectVideoCount(year+"-05-"));
        countList.add(managementMapper.selectVideoCount(year+"-06-"));
        countList.add(managementMapper.selectVideoCount(year+"-07-"));
        countList.add(managementMapper.selectVideoCount(year+"-08-"));
        countList.add(managementMapper.selectVideoCount(year+"-09-"));
        countList.add(managementMapper.selectVideoCount(year+"-10-"));
        countList.add(managementMapper.selectVideoCount(year+"-11-"));
        countList.add(managementMapper.selectVideoCount(year+"-12-"));
        map.put("msg","成功");
        map.put("total","统计每月视频数量");
        map.put("count",countList);
        return map;
    }

    /**
     * 统计每月视频数量
     * @param video
     * @return
     */
    public List<Map<String,Object>> selectVideoCount2(Video video)
    {
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));//算出当前年份
        String[] monthArry = new String[]{"-01-","-02-","-03-","-04-","-05-","-06-","-07-","-08-","-09-","-10-","-11-","-12-"};

        List<Map<String, Object>> countList = new ArrayList<>();
        for(int i=0;i<monthArry.length;i++)
        {
            Map<String, Object> map = new HashMap<>();
            map.put("value",managementMapper.selectVideoCount2(year + monthArry[i]));
            map.put("name",(i+1) + "月");
            countList.add(map);
        }

        return countList;
    }

    /**
     * 统计个人每月视频数量
     * @param video
     * @return
     */
    public List<Map<String,Object>> selectPersionalVideoCount(Video video)
    {
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));//算出当前年份
        String[] monthArry = new String[]{"-01-","-02-","-03-","-04-","-05-","-06-","-07-","-08-","-09-","-10-","-11-","-12-"};

        List<Map<String, Object>> countList = new ArrayList<>();
        for(int i=0;i<monthArry.length;i++)
        {
            Map<String, Object> map = new HashMap<>();
            map.put("value",managementMapper.selectPersionalVideoCount(year + monthArry[i],video.getUserId()));
            map.put("name",(i+1) + "月");
            countList.add(map);
        }

        return countList;
    }

}
